package com.shenglue.ssi.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.shenglue.ssi.domain.BuyInDetail;


public class BuyInDetailDaoImpl extends SqlMapClientDaoSupport implements BuyInDetailDao {

	/**
	 * 查询某入库单的 所有入库单细表信息
	 */
	public List<BuyInDetail> selectAllBuyInDetail( int buyInId ) {
		@SuppressWarnings("unchecked")
		List<BuyInDetail> list = this.getSqlMapClientTemplate().queryForList(
				"selectAllBuyInDetail", buyInId );

		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 增加入库单细表记录
	 */
	public boolean saveBuyInDetail( BuyInDetail buyInDetail ) {

		try {
			Object oRtn = this.getSqlMapClientTemplate().insert( "insertBuyInDetail", buyInDetail );
			// 已改正 来自网上的写法。已有文章讨论

			//插入成功 返回true
			return true;
		}
		catch ( DataAccessException e ) {
			//失败 返回false
			return false;
		}
	}

	/**
	 * 删除入库单细表记录
	 */
	public boolean deleteBuyInDetail( int buyInDetailId ) {
		int flag= this.getSqlMapClientTemplate().delete("deleteBuyInDetailById", buyInDetailId);
		if( flag > 0 ) {
			return true;
		}
		return false;
	}

	/**
	 * 修改入库单细表记录
	 */
	public boolean updateBuyInDetail( BuyInDetail buyInDetail ) {
		int flag = this.getSqlMapClientTemplate().update("updateBuyInDetail", buyInDetail);
		if( flag > 0 ) {
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 为修改，根据 Id 查一条 入库单子表记录
	 */
	public BuyInDetail getOneBuyInDetailForUpdate( int buyInDetailId ) {
		Object oBuyInDetail = this.getSqlMapClientTemplate().
		queryForObject( "getOneBuyInDetailForUpdate", buyInDetailId );

		BuyInDetail buyInDetail = (BuyInDetail)oBuyInDetail;

		return buyInDetail;

	}
}
